package com.hh.bss.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class URLHelper {
	/**
	 * 取得某URL的信息流
	 * @param url URL : 网点URL
	 * @return InputStream : 信息流
	 * @throws IOException : 
	 */
	public static InputStream getDocumentAsInputStream(final URL url) throws IOException{
		InputStream in = url.openStream();
		return in;
	}
	/**
	 * 取得某URL的信息流
	 * @param url String : 网点URL字符串表示
	 * @return　InputStream : 信息流
	 * @throws MalformedURLException : 当字符串URL为错误的URL地址时抛出
	 * @throws IOException :
	 */
	public static InputStream getDocumentAsInputStream(final String url) throws MalformedURLException,IOException{
		URL u = new URL(url);		
		return getDocumentAsInputStream(u);
	}
	/**
	 * 得到某网点的所有信息
	 * @param url URL : 网点URL 
	 * @return String : 网点上的所有信息
	 * @throws IOException : 
	 */
	public static String getDocumentAsString(final URL url) throws IOException{
		StringBuffer result = new StringBuffer();
		InputStream in = url.openStream();		
		int c;
		while((c = in.read()) != -1){
			result.append((char) c);
		}
		return result.toString();
	}
	/**
	 * 得到某网点的所有信息
	 * @param url String : 网点URL字符串表示
	 * @return String : 网点上的所有信息
	 * @throws MalformedURLException : 当字符串URL为错误的URL地址时抛出
	 * @throws IOException : 
	 */
	public static String getDocumentAsString(final String url) throws MalformedURLException,IOException{
		URL u = new URL(url);
		return getDocumentAsString(u);
	}
	public URLHelper(){
	}
	public static void main(String[] args) throws MalformedURLException, IOException{
//		System.out.println(URLHelper.getDocumentAsString("http://adf.netmovie.com.cn:8080/rtshow?link=http%3A%2F%2Fadf.netmovie.com.cn%3A8080%2Fadfclick%3Fsession%3D200703231816086279924450%26link%3Dhttp%3A%2F%2Fwww.netfilm.cn%2F"));
		System.out.println(URLEncoder.encode("<font>"));
		
//		System.out.println(EncodeHelper.encode(URLHelper.getDocumentAsString("http://auth.netmovie.com.cn/http/showConfig.do?id=3"),"ISO8859_1","GBK"));
	}
}
